package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddArticleDto;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.service.IYddArticleService;
import com.itheima.ydd.service.IYddCateService;
import com.itheima.ydd.service.IYddTagService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.jws.Oneway;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_article")
@Slf4j
public class YddArticleController {

    @Autowired
    private IYddArticleService iYddArticleService;

    @Autowired
    private IYddCateService iYddCateService;

    private IYddTagService iYddTagService;


    //分页查询
    @GetMapping("/list")
    public ResultData page(Long start_time, Long end_time, String title, Integer cateid, Integer page, Integer limit) {
        //分页构建器查询对象
        Page<YddArticle> pageInfo = new Page<>(page, limit);
        //条件构造器
        LambdaQueryWrapper<YddArticle> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加标题模糊查询条件 (时间不为空)
        if (start_time != null && end_time != null) {
            //开始结束时间之间
            lambdaQueryWrapper.between(YddArticle::getCreateDate, start_time, end_time);
        }
        //查询文章标题  StringUtils.isNotEmpty(title)字符串不为空
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(title), YddArticle::getTitle, title);
        //文章分类
        lambdaQueryWrapper.eq(cateid != null, YddArticle::getCateid, cateid);
        //查询基础数据
        lambdaQueryWrapper.orderByDesc(YddArticle::getCreateDate);
        //条件查询
        iYddArticleService.page(pageInfo, lambdaQueryWrapper);

        HashMap<String, Object> map = new HashMap<>();
        map.put("count", pageInfo.getTotal());
        map.put("totalPages", pageInfo.getPages());
        map.put("pageSize", pageInfo.getSize());

        //构建YddArticleDto集合
        List<YddArticleDto> list = new ArrayList<>();
        //查询并遍历yddArticle中的所有数据
        List<YddArticle> yddArticleList = pageInfo.getRecords();
        //Stream仅仅代表着数据流,并没有数据结构,所以他遍历完一次之后便再也无法遍历
        //stream.map是将List中的每一个organization元素拆分作为一个map来处理
        list = yddArticleList.stream().map((item) -> {
            //查询分类名称(Cateid:文章分类id)  拿到id
            Integer cateid1 = item.getCateid();
            //用得到的文章分类id查询
            YddCate yddCate = iYddCateService.getById(cateid1);
            //得到文章分类名
            String catename = null;
            if (yddCate != null) {
                catename = yddCate.getCatename();
            }
            //拷贝
            YddArticleDto yddArticleDto = new YddArticleDto();
            BeanUtils.copyProperties(item, yddArticleDto);
            yddArticleDto.setCatename(catename);
            return yddArticleDto;
            // .collect(Collectors.toList()) 转换成list集合
        }).collect(Collectors.toList());
        map.put("data", list);
        return ResultData.ok("", map);
    }

    //数据回显
    @GetMapping("rowInfo")
    public ResultData rowInfo(int id) {
        YddArticle byId = iYddArticleService.getById(id);
        YddArticle article = new YddArticle();
        BeanUtils.copyProperties(byId, article);
        log.info("create_date:{}", byId.getCreateDate());

        return ResultData.ok("", article);
    }

    //删除
    @GetMapping("del")
    public ResultData del(int id) {

//        LambdaQueryWrapper<YddArticle> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        //拉姆达表达式进行文章id对比
//        lambdaQueryWrapper.eq(YddArticle::getId, id);
//        //调用service返回对象
//        YddArticle yddArticle = iYddArticleService.getOne(lambdaQueryWrapper);
//        //这里注意*** 先将标签以字符串形式返回 再进行判断
//        //直接判断可能会出现空指针异常
//        String tags = yddArticle.getTags();
//        log.info("//////////////////////////////////////t{}",tags);
//        if (tags != null) {
//            return ResultData.error("此表有关联数据,不允许删除");
//        }
        iYddArticleService.removeById(id);
        return ResultData.ok("删除成功", "");
    }

    //添加
    @PostMapping("/add")
    private ResultData add(@RequestBody YddArticle yddArticle) {
        iYddArticleService.save(yddArticle);
        return ResultData.ok("文章添加成功", "");
    }

    //修改
    @PostMapping("update")
    private ResultData update(@RequestBody YddArticle yddArticle) {
        iYddArticleService.updateById(yddArticle);
        return ResultData.ok("文章更新成功", "");
    }
}
